/*
    Android UIML

	Copyright (C) 2010  Bram Goffings (bramgoffings@gmail.com)

	This program is free software; you can redistribute it and/or
	modify it under the terms of the GNU Lesser General Public License
	as published by the Free Software Foundation; either version 2.1
	of	the License, or (at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU Lesser General Public License for more details.

	You should have received a copy of the GNU Lesser General Public License
	along with this program; if not, write to the Free Software
	Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/

package uiml.android.elements;

import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;

import uiml.android.template.Template;
import uiml.android.template.TemplateRepository;
import uiml.android.template.TemplateResolver;

public abstract class UimlElementWithAttributes extends UimlElement implements IUimlAttribute{

	// TODO comment
	protected String identifier = null;
	protected String source = null;
	protected String how = "replace";
	protected String export = "optional";

	// TODO comment
	public enum HOW_VALS { Union,  Cascade,  Replace };
	public static final String REPLACE = "replace";
	public static final String CASCADE = "cascade";
	public static final String UNION   = "union";
	
	// TODO comment
	public enum EXPORT_VALS { Hidden, Optional, Required };
	public final String HIDDEN 		= "hidden";
	public final String OPTIONAL 	= "optional";
	public final String REQUIRED 	= "required";
	
	// TODO comment
	public final String ID 		= "id";
	public final String SOURCE 	= "source";
	public final String HOW		= "how";
	public final String EXPORT	= "export";
	
	
	/**
	 * Constructor
	 * 
	 * @param node
	 */
	protected UimlElementWithAttributes(Element node) {
		super(node);
	}
	
	/**
	 * Overschrijven van de algemene process functie
	 * Eerst lezen we de attributen in vooraleer we verder gaan 
	 * met processen van het element en zijn kinderen
	 */
	@Override
	public void process() {
		processAttributes();
		super.process();
		
		// Merge eventuele templates
		processSource();
	}
	
	/**
	 * comment
	 */
	@Override
	public void processAttributes() {
		NamedNodeMap attributes = node.getAttributes();
		
		if(attributes != null) {
			if(attributes.getNamedItem(ID) != null)
				identifier = attributes.getNamedItem(ID).getNodeValue();
			if(attributes.getNamedItem(SOURCE) != null)
				source = attributes.getNamedItem(SOURCE).getNodeValue();
			if(attributes.getNamedItem(HOW) != null)
				how = attributes.getNamedItem(HOW).getNodeValue();
			if(attributes.getNamedItem(EXPORT) != null)
				export = attributes.getNamedItem(EXPORT).getNodeValue();
		}	
	}
	
	/**
	 * 
	 */
	public void processSource() {
		
		if(source != null) {
			
			// Get the desired template
			Template t = TemplateRepository.getInstance().getTemplate(source);
			
			// If this template exist, try to add the content
			if(t!=null) {
				
				// Get the desired templateresolver
				// "How" atribute is never "NULL"
				TemplateResolver tempResolver = t.getTemplateResolver(how);
				
				// Resolve template
				// We merge de content of t with the current UimlElement in a specific way.
				// This way is determined by the TemplateResolver
				tempResolver.resolve(t, this);
			}
		}
	}
	
	/**
	 * Get identifier
	 * 
	 * @return the identifier
	 */
	public String getIdentifier() {
		return identifier;
	}
	
	/**
	 * Set new identifier
	 * 
	 * @param identifier the new identifier
	 */
	public void setIdentifier(String identifier) {
		this.identifier = identifier;
	}
	
	/**
	 * Get source
	 * 
	 * @return the source
	 */
	public String getSource() {
		return source;
	}
	
	/**
	 * Set new source
	 * 
	 * @param source the new source
	 */
	public void setSource(String source) {
		this.source = source;
	}
	
	/**
	 * Get how
	 * 
	 * @return the how
	 */
	public String getHow() {
		return how;
	}
	
	/**
	 * Set new how
	 * 
	 * @param how the new how
	 */
	public void setHow(String how) {
		this.how = how;
	}
	
	/**
	 * Get export
	 * 
	 * @return the export
	 */
	public String getExport() {
		return export;
	}
	
	/**
	 * Set new export
	 * 
	 * @param export the new export
	 */
	public void setExport(String export) {
		this.export = export;
	}
	

}
